 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digitaldiary.bean;

import com.digitaldiary.entity.DiaryUser;
import java.awt.Component;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;

import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.jms.Session;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;


/**
 * @author Shaiful Islam (palash)
 */
@ManagedBean(name = "loginHandler")
@SessionScoped
public class LoginHandler {
    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    @Resource
    private UserTransaction userTransaction;
    
    @PostConstruct
    public void init() {}
    
  DiaryUser user = null;
  private String loggedUser;

  
  public void login(){
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        try{
            List<DiaryUser> userList = null;
            Query query = entityManager.createQuery("Select s from DiaryUser s where s.userName=:userName and s.password1=:password");
            query.setParameter("userName", userName);
            query.setParameter("password", password);
            userList = query.getResultList();
            for(DiaryUser auser : userList){
                // saving Logged in user into session
                FacesContext context = FacesContext.getCurrentInstance();
                context.getExternalContext().getSessionMap().put("diaryUser", auser);
                System.out.println("successful login");
                String url = "faces/home.xhtml";
                FacesContext fc = FacesContext.getCurrentInstance();
                ExternalContext ec = fc.getExternalContext();
                try {
                    ec.redirect(url);
                } catch (Throwable ex) {
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

   public void login1(){
          EntityManager entityManager = entityManagerFactory.createEntityManager();
         
        try{
            
            Query query = entityManager.createQuery("Select s from DiaryUser s where s.userName=:userName and s.password1=:password");
            query.setParameter("userName", userName);
            query.setParameter("password", password);
            user = (DiaryUser) query.getSingleResult();
            System.out.println(user.getEmail());
            FacesContext context = FacesContext.getCurrentInstance();
               
            if(user!=null){
                System.out.println(" jlkjlkjlk");
                 context.getExternalContext().getSessionMap().put("diaryUser", user);
                 System.out.println("successful login");
                 String url = "faces/home.xhtml";
                 FacesContext fc = FacesContext.getCurrentInstance();
                 ExternalContext ec = fc.getExternalContext();
                 try {
                     ec.redirect(url);
                 } catch (Throwable ex) {
                 }
           }else{
                
               System.out.println("6465465456");
               FacesMessage message= new FacesMessage(FacesMessage.SEVERITY_WARN,"Error: ", "User name or password is incorrect");
               context.addMessage(null, message);
               
           }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
   
  public void logout() {
        System.out.println("Signout");
        HttpSession session = (HttpSession)
            FacesContext.getCurrentInstance().getExternalContext().getSession(false);       
            session.invalidate();
            String url = "faces/index.xhtml";
                 FacesContext fc = FacesContext.getCurrentInstance();
                 ExternalContext ec = fc.getExternalContext();
                 try {
                     ec.redirect(url);
                 } catch (Throwable ex) {
                 }
   
    }

    public String getLoggedUser() {
        return loggedUser;
    }
    
    
    public String userName;
    public String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
}
